package br.com.universidade.servlets.professor;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.universidade.bean.Professor;
import br.com.universidade.dao.ProfessorDao;
import br.com.universidade.servlets.GenericServlet;
import br.com.universidade.util.enun.ViewEnun;

@WebServlet(urlPatterns = "/saveProfessor")
public class CadastrarProfessorServlet extends GenericServlet {
	private static final long serialVersionUID = 1L;
	private static final String PROFESSOR = "professor";
	private static final String MSG_OBRIGAOTRIA =  "Campo obrigatorio";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		req.setAttribute(ACTION_FORM,"saveProfessor");
		req.getRequestDispatcher(JSP_CADASTR_PROFESSOR).forward(req, resp);
		req.setAttribute("cpfError", "");
		req.setAttribute("nomeError", "");
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {

		Professor professor = preencherEntity(req);
		try {
			ProfessorDao dao = new ProfessorDao();
			validarCampos(req,professor);
			dao.save(professor);
			req.setAttribute(ViewEnun.MSG_SUCESS.getValue(),"Operacao Realizada com sucesso");
		} catch (NumberFormatException e) {
			req.setAttribute(ViewEnun.MSG_ERROR.getValue(), e.getMessage());
		} catch (Exception e) {
			req.setAttribute(ViewEnun.MSG_ERROR.getValue(), e.getMessage());
			e.printStackTrace();
		} finally {
			req.setAttribute(PROFESSOR, professor);
			redirect(req, resp);
		}
	}

	public static void validarCampos(HttpServletRequest req, Professor professor)throws Exception {
		boolean aconteceuErro = false;
		if(isFilled(professor.getCpf())){
			aconteceuErro = true;
			req.setAttribute("cpfError", MSG_OBRIGAOTRIA);
		}
		
		if(isFilled(professor.getNome())){
			aconteceuErro = true;
			req.setAttribute("nomeError", MSG_OBRIGAOTRIA);
		}
		
		if(aconteceuErro)throw new NumberFormatException("Campo obrigatorio nao preenchido");
	}

	private void redirect(HttpServletRequest req, HttpServletResponse resp) {
		try {
			req.getRequestDispatcher(JSP_CADASTR_PROFESSOR).forward(req, resp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Professor preencherEntity(HttpServletRequest req) {
		String id = req.getParameter(ViewEnun.Professor.ID.getValue());
		String nome = req.getParameter(ViewEnun.Professor.NOME.getValue());
		String cpf = req.getParameter(ViewEnun.Professor.CPF.getValue());
		String endereco = req.getParameter(ViewEnun.Professor.EDERECO.getValue());
		String email = req.getParameter(ViewEnun.Professor.EMAIL.getValue());
		String telefone = req.getParameter(ViewEnun.Professor.TELEFONE.getValue());

		
		if (isInteger(id) && id != null && !id.isEmpty()) {
			return new Professor(Integer.parseInt(id), cpf, nome, endereco,email, telefone);
		}
		return new Professor(cpf, nome, endereco, email, telefone);
	}

	
	
	
	
}
